//
//  PopulationCensusViewController.m
//  魔兽大数据
//
//  Created by 王右 on 2017/11/14.
//  Copyright © 2017年 王右. All rights reserved.
//

#import "PopulationCensusViewController.h"
#import "CensusTableViewCell.h"
#import "ServerPlayerListModel.h"
#import "UIViewController+NetworkHUD.h"
#import "BigDataProportionView.h"
#import "BigDataHeader.h"

@interface PopulationCensusViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
//源数据
@property (nonatomic, strong) NSArray *dataArr;
//展示tableView
@property (weak, nonatomic) IBOutlet UITableView *baseTableView;
//header
@property (strong, nonatomic) IBOutlet UIView *headerView;
//搜索框
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
//搜索结果数组
@property (nonatomic, strong) NSMutableArray *searchListArr;
//搜索字符串
@property (nonatomic, strong) NSString *searchString;
//回到顶部button
@property (weak, nonatomic) IBOutlet UIButton *backToTopButton;
@end

@implementation PopulationCensusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"人口普查";
    _backToTopButton.layer.cornerRadius = _backToTopButton.bounds.size.width / 2;
    _baseTableView.tableHeaderView = _headerView;
    [_searchBar.backGround removeFromSuperview];
    [self getData];
}

- (void)getData{
    [self hudWithText:@"加载中..."];
    [RequestManager requestGetBigDataWithModule:@"playerstatistics/findAll" param:nil model:@"ServerPlayerListModel" success:^(NSArray *obj) {
        [self removeHud];
        _dataArr = obj;
        _searchListArr = obj.mutableCopy;
        [_baseTableView reloadData];
    } failure:^(NSString *error) {
        [self removeHud];
    }];
    
    
//    [RequestManager requestTestSuccess:^(id obj) {
//        
//    } failure:^(NSString *error) {
//        
//    }]; 
//    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"serverList" ofType:@"plist"];
//    NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:filePath];
}



- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _searchListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CensusTableViewCell *cell = (CensusTableViewCell *)[tableView cellWithCellNibName:@"CensusTableViewCell"];
    ServerPlayerListModel *model = _searchListArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    NSString *searchString = searchBar.text;
    //搜索字符串未发生改变
    if ([_searchString isEqualToString:searchString]) {
        return;
    }
    //搜索关键字为空
    if (searchString.length == 0) {
        _searchListArr = _dataArr.mutableCopy;
        [_baseTableView reloadData];
        return;
    }
    _searchString = searchString;
    __block NSMutableArray *listArr = [NSMutableArray array];
    [_dataArr enumerateObjectsUsingBlock:^(ServerPlayerListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.realmName containsString:searchString]) {
            [listArr addObject:obj];
        }
    }];
    if (listArr.count > 0) {
        [_searchListArr removeAllObjects];
        [_searchListArr addObjectsFromArray:listArr];
        [_baseTableView reloadData];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
    if (scrollView.contentOffset.y > self.view.bounds.size.height) {
        _backToTopButton.hidden = NO;
    }else{
        _backToTopButton.hidden = YES;
    }
}
- (IBAction)backToTopButtonAction:(id)sender {
    NSIndexPath* indexPat = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.baseTableView scrollToRowAtIndexPath:indexPat atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
